<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"  %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	
</head>
<body class="no-skin">
<c:if test="${not empty message }">
	<script type="text/javascript">
		alert('${message}');
	</script>
</c:if>
	<div class="main-content">
				<!-- #section:basics/content.breadcrumbs -->
				<div class="breadcrumbs" id="breadcrumbs">
					<script type="text/javascript">
						try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
					</script>

					<ul class="breadcrumb">
						<li>
							<i class="ace-icon fa fa-home home-icon"></i>
							<a href="#">Home</a>
						</li>

						<li>
							<a href="#">Tables</a>
						</li>
						<li class="active">Simple &amp; Dynamic</li>
					</ul><!-- /.breadcrumb -->

					<!-- #section:basics/content.searchbox -->
					<div class="nav-search" id="nav-search">
						<form class="form-search">
							<span class="input-icon">
								<input type="text" placeholder="Search ..." class="nav-search-input" id="nav-search-input" autocomplete="off" />
								<i class="ace-icon fa fa-search nav-search-icon"></i>
							</span>
						</form>
					</div><!-- /.nav-search -->

					<!-- /section:basics/content.searchbox -->
				</div>

				<!-- /section:basics/content.breadcrumbs -->
				<div class="page-content">
					<!-- #section:settings.box -->
					<div class="ace-settings-container" id="ace-settings-container">
						<div class="btn btn-app btn-xs btn-warning ace-settings-btn" id="ace-settings-btn">
							<i class="ace-icon fa fa-cog bigger-150"></i>
						</div>

						<div class="ace-settings-box clearfix" id="ace-settings-box">
							<div class="pull-left width-50">
								<!-- #section:settings.skins -->
								<div class="ace-settings-item">
									<div class="pull-left">
										<select id="skin-colorpicker" class="hide">
											<option data-skin="no-skin" value="#438EB9">#438EB9</option>
											<option data-skin="skin-1" value="#222A2D">#222A2D</option>
											<option data-skin="skin-2" value="#C6487E">#C6487E</option>
											<option data-skin="skin-3" value="#D0D0D0">#D0D0D0</option>
										</select>
									</div>
									<span>&nbsp; Choose Skin</span>
								</div>

								<!-- /section:settings.skins -->

								<!-- #section:settings.navbar -->
								<div class="ace-settings-item">
									<input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-navbar" />
									<label class="lbl" for="ace-settings-navbar"> Fixed Navbar</label>
								</div>

								<!-- /section:settings.navbar -->

								<!-- #section:settings.sidebar -->
								<div class="ace-settings-item">
									<input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-sidebar" />
									<label class="lbl" for="ace-settings-sidebar"> Fixed Sidebar</label>
								</div>

								<!-- /section:settings.sidebar -->

								<!-- #section:settings.breadcrumbs -->
								<div class="ace-settings-item">
									<input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-breadcrumbs" />
									<label class="lbl" for="ace-settings-breadcrumbs"> Fixed Breadcrumbs</label>
								</div>

								<!-- /section:settings.breadcrumbs -->

								<!-- #section:settings.rtl -->
								<div class="ace-settings-item">
									<input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-rtl" />
									<label class="lbl" for="ace-settings-rtl"> Right To Left (rtl)</label>
								</div>

								<!-- /section:settings.rtl -->

								<!-- #section:settings.container -->
								<div class="ace-settings-item">
									<input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-add-container" />
									<label class="lbl" for="ace-settings-add-container">
										Inside
										<b>.container</b>
									</label>
								</div>

								<!-- /section:settings.container -->
							</div><!-- /.pull-left -->

							<div class="pull-left width-50">
								<!-- #section:basics/sidebar.options -->
								<div class="ace-settings-item">
									<input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-hover" />
									<label class="lbl" for="ace-settings-hover"> Submenu on Hover</label>
								</div>

								<div class="ace-settings-item">
									<input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-compact" />
									<label class="lbl" for="ace-settings-compact"> Compact Sidebar</label>
								</div>

								<div class="ace-settings-item">
									<input type="checkbox" class="ace ace-checkbox-2" id="ace-settings-highlight" />
									<label class="lbl" for="ace-settings-highlight"> Alt. Active Item</label>
								</div>

								<!-- /section:basics/sidebar.options -->
							</div><!-- /.pull-left -->
						</div><!-- /.ace-settings-box -->
					</div><!-- /.ace-settings-container -->

					<!-- /section:settings.box -->
					<div class="page-content-area">
						<div class="page-header">
							<h1>
								课程信息
								<small>
									<i class="ace-icon fa fa-angle-double-right"></i>
									Subject Message
								</small>
							</h1>
						</div><!-- /.page-header -->

						<div class="row">
							<div class="col-xs-12">
								<!-- PAGE CONTENT BEGINS -->
								<div class="row">
									<div class="col-xs-12">
										<table id="sample-table-1" class="table table-striped table-bordered table-hover">
											<thead>
												<tr>			
													<th align="center"><button type="button" class="btn btn-info btn-sm" id="saveSub">增加课程</button></th>
													<th>课程编号</th>
													<th>课程名</th>
													<th>课程描述</th>
													<th>测试的次数</th>
													<th>考试的总时间</th>
													<th>满分总分</th>
													<th>是否开课</th>
													<th>操作</th>
												</tr>
											</thead>

											<tbody>
												<c:forEach items="${subList.dates}" var="sub">
													<tr>
														<td class="center">
															<label class="position-relative">
																<input type="checkbox" class="ace">
																<span class="lbl"></span>
															</label>
														</td>
														<td>${sub.subId }</td>
														<td>${sub.name }</td>
														<td>${sub.description }</td>
														<td>${sub.testCount }</td>
														<td>${sub.totalTime }</td>
														<td>${sub.totalScore }</td>
														<c:if test="${sub.status==0 }">
															<td>
																<input name="switch-field-1" subid="${sub.subId }" class="ace ace-switch ace-switch-6 subject-status" type="checkBox"/>
																<span class="lbl"></span>
															</td>
														</c:if>
														<c:if test="${sub.status==1 }">
															<td>
																<input name="switch-field-1" checked="checked" subid="${sub.subId }" class="ace ace-switch ace-switch-6 subject-status" type="checkBox"/>
																<span class="lbl"></span>
															</td>
														</c:if>
														<td>
															<button type="button" class="btn btn-primary btn-xs updateSub" update_id="${sub.subId}"  title="修改科目">
																<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
															</button>
															<button type="button" class="btn btn-danger btn-xs deleteSub" delete_id="${sub.subId}"   title="删除科目">
																<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
															</button>
															<button type="button" class="btn btn-primary btn-xs manageSubStu"   title="管理科目学生">
																<span class="ace-icon fa fa-comment" aria-hidden="true"></span>
															</button>
														</td>
													</tr>
												</c:forEach>
											</tbody>
										</table>
										<pg:pager url="subject" items="${subList.totalSize}" export="currentPageNumber=pageNumber" maxPageItems="5">   
											  <pg:first>   
											    <a href="${pageUrl}">首页</a>   
											  </pg:first>   
											  <pg:prev>   
											    <a href="${pageUrl }">上一页</a>   
											  </pg:prev>   
											  <pg:pages>   
											    <c:choose>   
											      <c:when test="${currentPageNumber eq pageNumber}">   
											        <font color="red">${pageNumber }</font>   
											      </c:when>   
											      <c:otherwise>   
											        <a href="${pageUrl }">${pageNumber }</a>   
											      </c:otherwise>   
											    </c:choose>   
											  </pg:pages>   
											  <pg:next>   
											    <a href="${pageUrl }">下一页</a>   
											  </pg:next>   
											  <pg:last>   
											    <a href="${pageUrl }">尾页</a>   
											  </pg:last>   
										</pg:pager>   
										
									</div><!-- /.span -->
								</div><!-- /.row -->
							<div class="hr hr-18 dotted hr-double"></div>
						</div><!--col-xs-12-->
					</div><!--row-->
				</div><!-- /.page-content-area -->
			</div><!-- /.page-content -->
		</div><!-- /.main-content -->
		
		<!-- 添加课程 -->
		<div class="modal fade" tabindex="-1" role="dialog" id="addSubModal">
			  <div class="modal-dialog" role="document">
				    <div class="modal-content">
				    
					      <div class="modal-header">
						        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						        <h4 class="modal-title">添加课程</h4>
					      </div>
					      
					      <div class="modal-body" >
						      	<form id="addSubForm" method="post" class="form-horizontal">
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">课程名</label>
									    <div class="col-sm-10">
									      <input type="text" name="name" class="form-control" id="inputEmail3" placeholder="课程名">
									    </div>
								    </div>
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">详细</label>
									    <div class="col-sm-10">
									      	<input type="text" name="description" class="form-control" id="inputEmail3" placeholder="详细">
									    </div>
								    </div>
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">考试次数</label>
									    <div class="col-sm-10">
									      	<input type="text" name="testCount" class="form-control" id="inputEmail3" placeholder="测试次数">
									    </div>
								    </div>
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">考试总时间</label>
									    <div class="col-sm-10">
									      	<input type="text" name="totalTime" class="form-control" id="inputEmail3" placeholder="考试总时长">
									    </div>
								    </div>
								    
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">科目总分</label>
									    <div class="col-sm-10">
									     	<input type="text" name="totalScore" class="form-control" id="inputEmail3" placeholder="科目总分">
									    </div>
								    </div>
								    
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">是否开考</label>
									    <div class="col-sm-10">
												<input name="status" subid="${sub.subId }" id="addSubCheckBox" class="ace ace-switch ace-switch-6 subject-status" type="checkBox" />
												<span class="lbl"></span>
									    </div>
								    </div>		        	
						        </form>
					      </div>
					      
					      <div class="modal-footer">
						        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>	
						        <button type="button" class="btn btn-primary" data-dismiss="modal" id="addSubSaveBtn">保存</button>	
					      </div>
				    </div><!-- /.modal-content -->
			  </div><!-- /.modal-dialog -->
		</div><!-- /.modal -->
		
		
		
		<!-- 修改课程 -->
		<div class="modal fade" tabindex="-1" role="dialog" id="updateSubModal">
			  <div class="modal-dialog" role="document">
				    <div class="modal-content">
					      <div class="modal-header">
						        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						        <h4 class="modal-title">修改课程</h4>
					      </div>
					      
					      <div class="modal-body" >
						      	<form id="addSubForm" method="post" class="form-horizontal">
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">课程名</label>
									    <div class="col-sm-10">
									      <input type="text" name="name" class="form-control" id="updateSubName" placeholder="课程名">
									    </div>
								    </div>
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">详细</label>
									    <div class="col-sm-10">
									      	<input type="text" name="description" class="form-control" id="updateSubDescription" placeholder="详细">
									    </div>
								    </div>
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">考试次数</label>
									    <div class="col-sm-10">
									      	<input type="text" name="testCount" class="form-control" id="updateSubTestCount" placeholder="测试次数">
									    </div>
								    </div>
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">考试总时间</label>
									    <div class="col-sm-10">
									      	<input type="text" name="totalTime" class="form-control" id="updateSubTotalTime" placeholder="考试总时长">
									    </div>
								    </div>
								    
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">科目总分</label>
									    <div class="col-sm-10">
									     	<input type="text" name="totalScore" class="form-control" id="updateSubTotalScore" placeholder="科目总分">
									    </div>
								    </div>
								    
								    
								    <div class="form-group">
									    <label for="inputEmail3" class="col-sm-2 control-label">是否开考</label>
									    <div class="col-sm-10">
												<input name="status" id="updateSubCheckBox" class="ace ace-switch ace-switch-6 subject-status" type="checkBox" />
												<span class="lbl"></span>
									    </div>
								    </div>		        	
						        </form>
					      </div>
					      
					      <div class="modal-footer">
						        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>	
						        <button type="button" class="btn btn-primary" data-dismiss="modal" id="updateSubBtn">修改</button>	
					      </div>
				    </div><!-- /.modal-content -->
			  </div><!-- /.modal-dialog -->
		</div><!-- /.modal -->
		
		<!-- 管理学生科目-->
		<div class="modal fade" tabindex="-1" role="dialog" id="subjectStudentModal">
			  <div class="modal-dialog" role="document">
				    <div class="modal-content">
					      <div class="modal-header">
						        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						        <h4 class="modal-title">
						        	管理学生科目:<label id="modalSubjectName"></label>
						        </h4>
					      </div>
					      
					      <div class="modal-body" >
						      	<input type="hidden" id="subjectId">
						      	<div id="subjectStudentContent" class="control-group">
						      		
						      	</div>
					      </div>
					      
					      <div class="modal-footer">
						        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>	
						        <button type="button" class="btn btn-primary" data-dismiss="modal" id="subjectStudentSaveBtn">保存</button>	
					      </div>
				    </div><!-- /.modal-content -->
			  </div><!-- /.modal-dialog -->
		</div><!-- /.modal -->
		
<script type="text/javascript">
//点击弹出添加课程的模态框
$("#saveSub").click(function(){
	$("#addSubModal").modal({
		backdrop:"static"
	});
})

//增加课程
$("#addSubSaveBtn").click(function(){
	var status;
	var name=$("input[name='name']").val();
	var description=$("input[name='description']").val();
	var test_count=$("input[name='testCount']").val();
	var total_time=$("input[name='totalTime']").val();
	var total_score=$("input[name='totalScore']").val();
	if($("input[name='status']").is(':checked')==true){
		status=1;
	}
	else{
		status=0;
	}
	$.ajax({
		url:"${pageContext.request.contextPath }/Subjects/saveSub",
		type:"POST",
		data:{
			name:name,
			description:description,
			testCount:test_count,
			totalTime:total_time,
			totalScore:total_score,
			status:status
		},
		dataType:"json",
		success:function(result){
			$("#addSubModal").modal("hide");
			alert(result.message);
			window.location.reload();
		}
	})
})

//获得一条的课程数据并且打开模态框
$(".updateSub").click(function(){
	//获取一条课程数据
	$.ajax({
		url:"${pageContext.request.contextPath }/Subjects/getSubById",
		type:"GET",
		data:"sub_id="+$(this).attr("update_id"),
		success:function(result){
			$("#updateSubName").val(result.map.subject.name)
			$("#updateSubDescription").val(result.map.subject.description)
			$("#updateSubTestCount").val(result.map.subject.testCount)
			$("#updateSubTotalTime").val(result.map.subject.totalTime)
			$("#updateSubTotalScore").val(result.map.subject.totalScore)
			if(result.map.subject.status==1){
				$("#updateSubCheckBox").prop("checked",true);
			}
			else{
				$("#updateSubCheckBox").prop("checked",false);
			}
		}
	});
	//打开模态框
	$("#updateSubModal").modal({
		backdrop:"static"
	});
})

//修改功能请求
$("#updateSubBtn").click(function(){
	var status;
	var name=$("#updateSubName").val();
	var description=$("#updateSubDescription").val();
	var test_count=$("#updateSubTestCount").val();
	var total_time=$("#updateSubTotalTime").val();
	var total_score=$("#updateSubTotalScore").val();
	if($("#updateSubCheckBox").is(':checked')==true){
		status=1;
	}
	else{
		status=0;
	}
	$.ajax({
		url:"${pageContext.request.contextPath }/Subjects/updateSub",
		type:"POST",
		data:{
			subId:$(".updateSub").attr("update_id"),
			name:name,
			description:description,
			testCount:test_count,
			totalTime:total_time,
			totalScore:total_score,
			status:status
		},
		dataType:"json",
		success:function(result){
			$("#updateSubModal").modal("hide");
			alert(result.message);
			window.location.reload();
		}
	})
})

//删除功能
$(".deleteSub").click(function(){
	var subName=$(this).parent().siblings().eq(2).text();
	var subId=$(this).attr("delete_id")
	if(confirm("确认删除【"+subName+"】吗？")){
		$.ajax({
			url:"${pageContext.request.contextPath }/Subjects/deleteSub",
			type:"POST",
			data:"id="+subId,
			success:function(result){
				alert(result.message);
				window.location.reload();
			}
		})
	}
})

//根据科目的Id查询学生的信息
$(".manageSubStu").click(function(){
	var subId=$(this).parent().siblings().eq(1).text();
	$.ajax({
		type:"POST",
		url:"${pageContext.request.contextPath }/Subjects/getStuBySubId",
		data:"subId="+subId,
		success:function(result){
			var htmlString='';
			var selectedUser=result.map.selectedUser;
			for(var i=0;i<result.map.allUser.length;i++){
				var user=result.map.allUser[i];
				var checked=checkSelectedUser(selectedUser,user)?'checked="checked"':'';
				htmlString +='<div class="checkbox">';
				htmlString +='	<label>';
				htmlString +='		<input name="subjectStudents" class="ace ace-checkbox-2" type="checkBox" value="'+user+'" '+checked+'/>';
				htmlString +='		<span class="lbl">'+user+'</span>';
				htmlString +='	</label>';
				htmlString +='</div>';
			}
			$('#subjectStudentContent').html(htmlString);
			$('#subjectId').val(subId);
		}
	});
	$('#modalSubjectName').text($(this).parents("td").siblings("td").eq(2).text());
	$('#subjectStudentModal').modal('show');
})

//判断相应的id是否选上
function checkSelectedUser(selectedUser,allUserId){
	for(var i=0;i<=selectedUser.length;i++){
		if(selectedUser[i]==allUserId){
			return true;
		}
	}
	return false;
}

//加上该科目的考试学生
$('#subjectStudentSaveBtn').on('click',function(){
	var subjectStudents=new Array();
	//获取当前选中的学生，本质是获取当前选中学生id
	for(var i=0;i<$('input[name="subjectStudents"]:checked').length;i++){
		subjectStudents[i]=$($('input[name="subjectStudents"]:checked')[i]).val();
	}
	$.ajax({
		type:'POST',
		url:"${pageContext.request.contextPath }/Subjects/saveSubjectstudent",
		data:{
			//课程的Id
			subId:$("#subjectId").val(),
			//学生集合的id
			subjectStudents:subjectStudents
		},
		dataType:'json',
		success:function(result){
			alert(result.message);
			$("#subjectStudentModal").modal("hide");
		}
	})
})
</script>
</body>


</html>